<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Role_module_model extends OC_Model {

	function get_all($params = array())
	{
		$this->db->select('SQL_CALC_FOUND_ROWS a.*,b.name as group_name', FALSE);
		$this->db->from('o_role_modules a');
		$this->db->join('o_groups b', 'b.group_id = a.group_id', 'left');

		if (isset($params['sort'], $params['order']))
			$this->db->order_by($params['sort'], $params['order']);

		if (isset($params['page'], $params['rows']))
			$this->db->limit($params['rows'], (intval($params['page']-1)*intval($params['rows'])));

		if (isset($params['query']) && $params['query'] != '')
		{
			$query = $params['query'];

			$likes = array_map(
				function($f) use ($query) { return "$f LIKE '%$query%'"; }, 
				array('b.name', 'a.module_name')
			);
			
			$this->db->where('('.implode(' OR ', $likes).')');
		}

		return $this->fetch_rows();
	}

	public function get_groups()
	{
		$this->db->select('group_id, name');
		return $this->db->get('o_groups')->result();
	}

	function add($data)
	{
		return $this->db->insert('o_role_modules', $data);
	}

	function edit($data, $keys)
	{
		return $this->db->update('o_role_modules', $data, $keys);
	}

	function delete($keys)
	{
		return $this->db->delete('o_role_modules', $keys);
	}

}

/* End of file role_module_model.php */
/* Location: ./applications/module_admin/role_module/models/role_module_model.php */